/**
 * add by yang.bo
 * 主机管理
 */
define(['module/component/views/FormView', 'module/component/views/GridView',
		'module/hostapp/action/HostAppAction',
		'module/hostapp/tools/tools',
		"text!module/hostapp/templates/HostView.html"],
        function (FormView, GridView, hostAppAction, tools, templates) {
	var mainView = ngc.View.extend({
		template: ngc.compile(templates),
		events: {
			'click #queryBtn' : 'queryBtnClick',
			'click #cancelBtn' : 'cancelBtnClick'
	    },
	    _form: function(){
		      return this.getView("#form_content");
		  },
		_grid: function(){
			  return this.getView("#grid_content");
	  	},
	    queryBtnClick: function(){
	    	var queryData = this._form().getValues();
	    	var sendData = {
		    		  type : global.edit,
		    		  queryData : queryData
		      }
	    	hostAppAction.queryHost(sendData, function(data){
	    		this._grid().reloadData(data);
	    	}.bind(this));
	    },
	    cancelBtnClick: function(){
	    	this._form().reset();
	    	this.queryBtnClick();
	    },
	    
		render: function () {
			 console.log("render");
	         this.$el.append(this.template({}));
	         this.initForm();
	    },
	     
	    initialize: function () {
	    	 console.log("init");
	    	 this.render();	    	
	    },
	    
	    initWidget:function(){
	    	 $("#tabs").tabs({
	                canClose:true,
	              paging: true,
	              autoResizable:true
	            });
	    },

	    afterRender: function () {
	    	this.initWidget();
	    },
	    
	    //主机管理form表单
	    initForm: function () {	
	    	var that = this;
			var config_elements = [
					{
						id : 'hostIp',
						type : 'text',
						label : '主机IP'
					},{
						id : 'hostType',
						type : 'select',
						label : '主机类型',
						enumCode : "hostType",
					},{
						id : 'location',
						type : 'text',
						label : '主机位置'
//					},{
//						id : 'responsibility',
//						type : 'text',
//						label : '责任人'
					}];
			this.setView("#form_content", new FormView({
				config : {
					elements : config_elements
				}
			}));
			this.getView("#form_content").on("viewRenderAfter", function() {
				$.when(this._getDropItemDynValue("hostType")).then(function(data){
					var typeData = tools.getGridFormatSelectTV(data.data, "VALUE", "NAME");
					that.initGrid(typeData);
				}.bind(this));
				this.on("inputChange", function(cfg, e) {
					if((cfg == "hostType" && this.getValues()["hostType"] != '' ) || 
							(cfg == "hostIp") ||
							(cfg == "location")){
						that.queryBtnClick();
					}
		    	});
			})
	    },
	    
	    //主机管理grid
	    initGrid: function (typeData){
	    	var _colModel = [{
	            name: 'id',
	            label: 'ID',
	            key: true,
	            hidden: true
	        }, {
	        	name: 'hostIp',
	        	label: '主机IP',
	        }, {
	        	name: 'hostType',
	        	label: '主机类型',
	        	edittype: "select",
	        	formatter: "select",
	        	editoptions: { value: typeData }
	        }, {
	        	name: 'hostAccount',
	        	label: '主机帐号',
	        }, {
	        	name: 'hostPassword',
	        	label: '主机密码',
	        }, {
	        	name: 'operateSystem',
	        	label: '操作系统',
	        }, {
	        	name: 'cpu',
	        	label: 'CPU',
	        }, {
	            name: 'memory',
	            label: '内存',
	        }, {
	        	name: 'surplusMemory',
	        	label: '内存剩余',
	        }, {
	        	name: 'disk',
	        	label: '存储',
	        }, {
	        	name: 'surplusDisk',
	        	label: '剩余存储',
	        }, {
	        	name: 'location',
	        	label: '主机位置',
	        }, {
	        	name: 'responsibility',
	        	label: '责任人',
//	        }, {
//	        	name: 'startDate',
//	        	label: '启用时间',
	        }, {
	        	name: 'tel',
	        	label: '联系电话',
	        }];
	    	if(!global.edit){
	    		_colModel.splice(3, 2);
	    	}
	    	var that = this;
			var opt = {
		        height: 500,
		        colModel: _colModel,
		        toolbar: [true, "bottom"]
			};
			this.setView("#grid_content", new GridView({
				config : {					
					grid : opt,
					callback:{
					}
				}
			}));
			this.renderViews("#grid_content");
	        this.getView("#grid_content").on("viewRenderAfter", function() {
	        	if(global.edit){
	        		this.navButtonAdd([{
	        			title:"新增",
	        			id:"grid_content_addIcon",
	        			buttonicon:"glyphicon glyphicon-plus",
	        			navpos:"bottombar",
	        			caption:"新增",
	        			onClick:function(){
	        				that.gridAdd();
	        			}
	        		}
	        		]);
	        		this.attachMenu([{id:'add',label:'添加数据',icon:'fa fa-plus-square fa-lg',index:0},
	        		                 {id:'update',label:'修改数据',icon:'fa-pencil-square-o fa-lg',index:0},
	        		                 {id:'delete',label:'删除数据',icon:'fa fa-trash fa-lg',index:0}]);
	        		
	        		this.on("onClickMenu",function(menuId,selId,menu,e){
	        			switch (menuId) {
	        			case 'add':
	        				that.gridAdd();
	        				break;
	        			case 'update':
	        				that.gridUpdate(selId);
	        				break;
	        			case 'delete':
	        				that.gridDel(selId);
	        				break;
	        			}
	        		});
	        	}
				that.queryBtnClick();
			});
	    },
	    
	    gridAdd: function(){
	    	var that = this;
	    	ngc.openView({
	    		url: "module/hostapp/views/HostEditView",
	    		viewOption: {
	    			type : "add"
	    		},
	    		width: "80%",
	    		title: "主机添加"
	    	}).then(function(view){
	    		view.on('editView', function(data){
	    			that.queryBtnClick();
	    		});
	    	});
	    },
	    
	    gridUpdate: function(selId){
	    	var that = this;
	    	if(selId == null){
	    		ngc.info('请先选择一条数据进行修改操作!');
	    		return;
	    	}
	    	var _rowData = this._grid().getRowData(selId);
	    	ngc.openView({
	    		url: "module/hostapp/views/HostEditView",
	    		viewOption: {
	    			type : "edit",
	    			rowData : _rowData
	    		},
	    		width: "80%",
	    		title: "主机信息修改"
	    	}).then(function(view){
	    		view.on('editView', function(data){
	    			that.queryBtnClick();
	    		});
	    	});
	    },
	    
	    gridDel: function(rowid){
	    	var that = this;
	    	if(rowid == null){
	    		ngc.info('请先选择一条数据进行删除操作!');
	    		return;
	    	}
	    	ngc.confirm('确认是否删除此条数据?').result.then(function() {
	    		$.when(hostAppAction.delHost(rowid,$.noop))
		 		.then(function(data){
		 			if(data.resultStat == 'SUCCESS'){
		 				ngc.success("删除成功")
		 				that._grid().delRow(rowid);
		 			}
	    		});
			}.bind(this));
	    }
	});
	return mainView;
});
